import axios from 'axios';
import {Message} from "element-ui";
import store from "@/store/index";

//定义全局默认配置
axios.defaults.headers.common['Authorization'] = store.getters.getToken;

const request = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 50000
});

request.interceptors.request.use(config => {

    const token = store.getters.getToken;
    if(token) {
        config.headers.common['Authorization'] = token;
    }
    return config;
}, error => {
    return Promise.reject(error);
});

request.interceptors.response.use(response => {
    if(!response.data.flag) {
        Message.warning(response.data.message);
    }
    return response;
}, err => {
    const resp = err.response.data;
    if(!resp.flag) {
        Message.error(resp.message);
    }
    return Promise.reject(err);
});

export default request
